<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改任务调度')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-dispatcher-edit" th:object="${ktDispatcher}">
        <input name="id" th:field="*{id}" type="hidden">
        <input name="dpRepoType" id="dpRepoType" th:value="*{dpRepoType}" type="hidden" required>
        <input name="dpId" id="dpId" th:value="*{dpId}" type="hidden" required>
        <div class="form-group">
            <label class="col-sm-3 control-label">仓库名称：</label>
            <div class="col-sm-8">
                <input id="repoName" name="repoName" th:field="*{repoName}" class="form-control" readonly type="text" disabled>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">调度名称：</label>
            <div class="col-sm-8">
                <input name="dpName" th:field="*{dpName}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group is-required">
            <label class="col-sm-3 control-label is-required">服务节点：</label>
            <div class="col-sm-8">
                <select id="carteId" name="carteId"  class="form-control" required>
                    <option th:each="node:${nodeList}" th:value="${node.nodeId}" th:text="${node.name}" th:selected="${node.nodeId == ktDispatcher.carteId}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">服务地址：</label>
            <div class="col-sm-8">
                <input name="carteUri" th:field="*{carteName}" class="form-control" type="text" id="carteUri" required readonly>
            </div>
        </div>
        <div class="form-group is-required">
            <label class="col-sm-3 control-label">参数组：</label>
            <div class="col-sm-8">
                <select id="paramsGroupId" name="paramsGroupId"  class="form-control">
                    <option></option>
                    <option th:each="pg:${pgList}" th:value="${pg.paramsGroupId}" th:text="${pg.paramsGroupName}" th:selected="${ktDispatcher.paramsGroupId==pg.paramsGroupId}"></option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">预警等级：</label>
            <div class="col-sm-8">
                <select id="warnLevel" name="warnLevel" th:with="type=${@dict.getType('warn_level')}" class="form-control" required>
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:selected="${ktDispatcher.warnLevel==dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">预警人员：</label>
            <div class="col-sm-8">
                <input id="warnMsgIds"th:field="*{warnMsgIds}" name="warnMsgIds" class="form-control" type="text" readonly>
                <button type="button" onclick="selectWarn()">选择预警人员</button>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: bootstrap-select-js" />
<script th:inline="javascript">
    var prefix = ctx + "busi/dispatcher";
    var nodeList = [[${nodeList}]];

    $(function (){
        $("#carteId").bind("change", function () {
            var carteId = $("#carteId option:selected").val();
            if (carteId === undefined || carteId === null || carteId === '') {
                $("#dpPath").val(null);
                return;
            } else {
                $("#dpPath").val();
            }

            for (var i = 0; i < nodeList.length; i++) {
                if (nodeList[i]["nodeId"] === carteId*1) {
                    $("#dpPath").val(nodeList[i]["nodeHost"]);
                }
            }

        });

    });

    $("#form-dispatcher-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            let serialize = $('#form-dispatcher-edit').serialize();
            $.operate.save(prefix + "/edit", serialize);
        }
    }

    $("input[name='createdTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='updatedTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    function selectWarn() {
        var title = "选择预警人员";
        var url = "/busi/monitor/user/selected?warnMsgIds="+$("#warnMsgIds").val();

        selectFiles(title, url, "selectWarn");
    }

    var selectFiles = function (title, url, type) {
        var width = "", height = "";
        // 如果是移动端，就使用自适应大小弹窗
        if ($.common.isMobile()) {
            width = 'auto';
            height = 'auto';
        } else {
            width = "800px";
            height = ($(window).height() - 50) + "px";
        }

        var index = top.layer.open({
            type: 2,
            area: [width, height],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: title,
            content: url,
            btn: ['确定', '关闭'],
            // 弹层外区域关闭
            shadeClose: true,
            yes: function(index, layero) {
                var iframeWin = layero.find('iframe')[0];
                var selectIds = iframeWin.contentWindow.submitHandler(index, layero);
                if (type === "selectTranOrJob") {
                    $("#dpIds").val(selectIds);
                } else if (type === "selectWarn") {
                    $("#warnMsgIds").val(selectIds);
                }

                $("#form-dispatcher-add").valid();
            },
            cancel: function(index) {
                return true;
            }
        });
        top.layer.full(index);
    }
</script>
</body>
</html>